//
//  RKURLSpec.m
//  RestKit
//
//  Created by Blake Watters on 6/29/11.
//  Copyright 2011 Two Toasters. All rights reserved.
//

#import "RKSpecEnvironment.h"
#import "RKURL.h"

@interface RKURLSpec : RKSpec
@end
    
@implementation RKURLSpec

- (void)itShouldNotExplodeBecauseOfUnicodeCharacters {
    NSException* failed = nil;
    @try {
        [RKURL URLWithBaseURLString:@"http://test.com" resourcePath:@"/places.json?category=Automóviles"];
    }
    @catch (NSException *exception) {
        failed = exception;
    }
    @finally {
        NSAssert((failed == nil), @"No exception should be generated by creating URL with Unicode chars");
    }
}

- (void)itShouldEscapeQueryParameters {
    NSDictionary* queryParams = [NSDictionary dictionaryWithObjectsAndKeys:@"What is your #1 e-mail?", @"question", @"john+restkit@gmail.com", @"answer", nil];
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test" queryParams:queryParams];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test?answer=john%2Brestkit%40gmail.com&question=What%20is%20your%20%231%20e-mail%3F")));
}

- (void)itShouldHandleNilQueryParameters {
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test" queryParams:nil];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test")));
}

- (void)itShouldHandleEmptyQueryParameters {
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test" queryParams:[NSDictionary dictionary]];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test")));
}

- (void)itShouldHandleResourcePathWithoutLeadingSlash {
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"test"];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test")));
}

- (void)itShouldHandleEmptyResourcePath {
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@""];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org")));
}

- (void)itShouldHandleBaseURLsWithAPath {
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org/this" resourcePath:@"/test" queryParams:nil];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/this/test")));
}

- (void)itShouldSimplifyURLsWithSeveralSlashes {
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org//this//" resourcePath:@"/test" queryParams:nil];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/this/test")));
}

- (void)itShouldPreserveTrailingSlash {
    RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test/" queryParams:nil];
    assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test/")));    
}

@end
 